package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateConverterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public void add(Member member) {
        if (member.getPassword()!=null){

        }
        memberDao.add(member);
    }

    @Override
    public Map getReport(Integer year) {
        List<Map> countEachMonth = memberDao.getCountEachMonth(year);
        ArrayList<Integer> months = new ArrayList<>();
        ArrayList<Long> counts = new ArrayList<>();
        for (Map map : countEachMonth) {
            Integer month= (Integer) map.get("month");
            Long count=(Long) map.get("count");
            months.add(month);
            counts.add(count);
        }
        Map<String,List> map=new HashMap<>();
        map.put("months",months );
        map.put("counts",counts );
        return map;
    }

    @Override
    public List<Map> findMemberSex() {
        List<Map> list=new ArrayList<>();
       Integer man=memberDao.findByMan();
       Integer woman= memberDao.findByWoman();
       Map<String,Object> map1=new HashMap<>();
       map1.put("name","男");
       map1.put("value",man);
       Map<String,Object> map2=new HashMap<>();
       map2.put("name","女");
       map2.put("value",woman);
       list.add(map1);
       list.add(map2);
        return list;
    }

    @Override
    public List<Map> findMemberAge() throws Exception {
        List<Map> list=new ArrayList<>();
        String nowTime=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Calendar calendar=Calendar.getInstance();
//        获取18年前的时间
        calendar.add(Calendar.YEAR,-18);
        String year18ago=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        calendar.add(Calendar.YEAR,-12);
        String year30ago=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        calendar.add(Calendar.YEAR,-15);
        String year45ago=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        calendar.add(Calendar.YEAR,-75);
        String year120ago=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        Map<String,String> map1 =new HashMap<>();
        map1.put("begin",year18ago);
        map1.put("end",nowTime);
        Integer age18= memberDao.getCountAgeNumber(map1);
        Map<String,String> map2 =new HashMap<>();
        map2.put("begin",year30ago);
        map2.put("end",year18ago);
        Integer age30= memberDao.getCountAgeNumber(map2);
        Map<String,String> map3 =new HashMap<>();
        map3.put("begin",year45ago);
        map3.put("end",year30ago);
        Integer age45= memberDao.getCountAgeNumber(map3);
        Map<String,String> map4 =new HashMap<>();
        map4.put("begin",year120ago);
        map4.put("end",year45ago);
        Integer age120= memberDao.getCountAgeNumber(map4);
        Map<String,Object> age0_18=new HashMap<>();
        age0_18.put("name","0-18岁");
        age0_18.put("value",age18);
        Map<String,Object> age18_30=new HashMap<>();
        age18_30.put("name","18-30岁");
        age18_30.put("value",age30);
        Map<String,Object> age30_45=new HashMap<>();
        age30_45.put("name","30-45岁");
        age30_45.put("value",age45);
        Map<String,Object> age45_120=new HashMap<>();
        age45_120.put("name","45岁上");
        age45_120.put("value",age120);
        list.add(age0_18);
        list.add(age18_30);
        list.add(age30_45);
        list.add(age45_120);
        return list;
    }

    @Override
    public Member findMemberByTel(String telephone) {
        return memberDao.findByTel(telephone);
    }
}
